<?php
namespace app\mini\controller\v1\client;

use app\common\library\exception\ApiException;
use app\common\library\MemberVo;
use app\common\logic\MemberLogic;
use app\common\model\Agent;
use app\mini\controller\Common;
use think\Env;
use think\Request;
use think\response\Json;

class Info extends Common
{

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    /**
     * 代理信息
     * @return \think\response\Json
     */
    public function agent(): Json
    {
        $res = $this->exception(function (){
            $params = request()->param();
            $info = Agent::get(['code'=>$params['code']]);
            $data = [];
            $data['type'] = 1;//绑定
            if (empty($info))
                throw new ApiException('未找到相关代理信息',404);
            if (!empty($info['member_id'])){
                if ($info['member_id'] != MemberVo::instance()->getMemberId())
                    throw new ApiException('该代理已被其他用户绑定',404);
                $data['type'] = 2;//解除绑定
            }
            $data['name'] = $info['nickname'];
            $data['avatar'] = Env::get('app.app_host').$info['avatar'];
            return $data;
        });
        return $this->response($res);
    }




}